{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Collecting scikit-opt\n",
      "  Using cached https://files.pythonhosted.org/packages/b1/c4/35919cabffc2b5c76a792fed3c0de3548a9eed3ed1a86f6f2a4d25a24680/scikit_opt-0.5.0-py3-none-any.whl\n",
      "Requirement already satisfied: scipy in /local/home/ningzesun/.local/lib/python3.6/site-packages (from scikit-opt) (1.3.2)\n",
      "Requirement already satisfied: numpy in /local/home/ningzesun/.local/lib/python3.6/site-packages (from scikit-opt) (1.17.4)\n",
      "Installing collected packages: scikit-opt\n",
      "Successfully installed scikit-opt-0.5.0\n"
     ]
    }
   ],
   "source": [
    "!pip install scikit-opt --user"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import gc\n",
    "import os\n",
    "from pathlib import Path\n",
    "import random\n",
    "import sys\n",
    "\n",
    "from tqdm import tqdm_notebook as tqdm\n",
    "import numpy as np # linear algebra\n",
    "import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n",
    "\n",
    "from sklearn import preprocessing\n",
    "from sklearn.model_selection import KFold\n",
    "\n",
    "from sklearn.metrics import mean_squared_error"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pandas.api.types import is_datetime64_any_dtype as is_datetime\n",
    "from pandas.api.types import is_categorical_dtype\n",
    "\n",
    "def reduce_mem_usage(df, use_float16=False):\n",
    "    \"\"\" iterate through all the columns of a dataframe and modify the data type\n",
    "        to reduce memory usage.        \n",
    "    \"\"\"\n",
    "    start_mem = df.memory_usage().sum() / 1024**2\n",
    "    print('Memory usage of dataframe is {:.2f} MB'.format(start_mem))\n",
    "    \n",
    "    for col in df.columns:\n",
    "        if is_datetime(df[col]) or is_categorical_dtype(df[col]):\n",
    "            # skip datetime type or categorical type\n",
    "            continue\n",
    "        col_type = df[col].dtype\n",
    "        \n",
    "        if col_type != object:\n",
    "            c_min = df[col].min()\n",
    "            c_max = df[col].max()\n",
    "            if str(col_type)[:3] == 'int':\n",
    "                if c_min > np.iinfo(np.int8).min and c_max < np.iinfo(np.int8).max:\n",
    "                    df[col] = df[col].astype(np.int8)\n",
    "                elif c_min > np.iinfo(np.int16).min and c_max < np.iinfo(np.int16).max:\n",
    "                    df[col] = df[col].astype(np.int16)\n",
    "                elif c_min > np.iinfo(np.int32).min and c_max < np.iinfo(np.int32).max:\n",
    "                    df[col] = df[col].astype(np.int32)\n",
    "                elif c_min > np.iinfo(np.int64).min and c_max < np.iinfo(np.int64).max:\n",
    "                    df[col] = df[col].astype(np.int64)  \n",
    "            else:\n",
    "                if use_float16 and c_min > np.finfo(np.float16).min and c_max < np.finfo(np.float16).max:\n",
    "                    df[col] = df[col].astype(np.float16)\n",
    "                elif c_min > np.finfo(np.float32).min and c_max < np.finfo(np.float32).max:\n",
    "                    df[col] = df[col].astype(np.float32)\n",
    "                else:\n",
    "                    df[col] = df[col].astype(np.float64)\n",
    "        else:\n",
    "            df[col] = df[col].astype('category')\n",
    "\n",
    "    end_mem = df.memory_usage().sum() / 1024**2\n",
    "    print('Memory usage after optimization is: {:.2f} MB'.format(end_mem))\n",
    "    print('Decreased by {:.1f}%'.format(100 * (start_mem - end_mem) / start_mem))\n",
    "    \n",
    "    return df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "path = '../../Resources/'\n",
    "test_df = pd.read_csv(path + 'test.csv')\n",
    "building_meta_df = pd.read_csv(path + 'building_metadata.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "leak_df = pd.read_feather('../../Large_output/leaking/leak.feather')\n",
    "leak_df.fillna(0, inplace=True)\n",
    "leak_df = leak_df[(leak_df.timestamp.dt.year > 2016) & (leak_df.timestamp.dt.year < 2019)]\n",
    "leak_df.loc[leak_df.meter_reading < 0, 'meter_reading'] = 0 # remove large negative values\n",
    "leak_df = leak_df[leak_df.building_id!=245]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/local/home/ningzesun/.local/lib/python3.6/site-packages/numpy/lib/arraysetops.py:568: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison\n",
      "  mask |= (ar1 == a)\n"
     ]
    }
   ],
   "source": [
    "sub_path = '../../Large_output/good_submission/'\n",
    "sample_submission1 = pd.read_csv(sub_path + 'lgb3_site0_change.csv', index_col=0)\n",
    "sample_submission2 = pd.read_csv(sub_path + 'lgb_half.csv', index_col=0)\n",
    "sample_submission3 = pd.read_csv(sub_path + 'lgb3_cleaned.csv', index_col=0)\n",
    "sample_submission4 = pd.read_csv(sub_path + 'cat_clean_no_bayes.csv', index_col=0)\n",
    "sample_submission5 = pd.read_csv(sub_path + 'xgb_no_bayes_clean.csv', index_col=0)\n",
    "sample_submission6 = pd.read_csv(sub_path + 'nn_clean.csv', index_col=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_df['pred1'] = sample_submission1.meter_reading\n",
    "test_df['pred2'] = sample_submission2.meter_reading\n",
    "test_df['pred3'] = sample_submission3.meter_reading\n",
    "test_df['pred4'] = sample_submission4.meter_reading\n",
    "test_df['pred5'] = sample_submission5.meter_reading\n",
    "test_df['pred6'] = sample_submission6.meter_reading\n",
    "\n",
    "del  sample_submission1,  sample_submission2,  sample_submission3,sample_submission4,sample_submission5,sample_submission6\n",
    "gc.collect()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_df[\"timestamp\"] = pd.to_datetime(test_df[\"timestamp\"], format='%Y-%m-%d %H:%M:%S')\n",
    "leak_df[\"timestamp\"] = pd.to_datetime(leak_df[\"timestamp\"], format='%Y-%m-%d %H:%M:%S')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "leak_df = leak_df.merge(test_df[['building_id', 'meter', 'timestamp', 'pred1',\\\n",
    "                                 'pred2', 'pred3','pred4','pred5','pred6',\\\n",
    "                                 'row_id']], on = ['building_id', 'meter', 'timestamp'], how = \"left\")\n",
    "leak_df = leak_df.merge(building_meta_df[['building_id', 'site_id']], on='building_id', how='left')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "leak_df['meter_reading_l1p'] = np.log1p(leak_df.meter_reading)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>building_id</th>\n",
       "      <th>meter</th>\n",
       "      <th>meter_reading</th>\n",
       "      <th>timestamp</th>\n",
       "      <th>pred1</th>\n",
       "      <th>pred2</th>\n",
       "      <th>pred3</th>\n",
       "      <th>pred4</th>\n",
       "      <th>pred5</th>\n",
       "      <th>pred6</th>\n",
       "      <th>row_id</th>\n",
       "      <th>site_id</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>173.370293</td>\n",
       "      <td>2017-01-01 00:00:00</td>\n",
       "      <td>196.794798</td>\n",
       "      <td>82.730302</td>\n",
       "      <td>134.285627</td>\n",
       "      <td>121.220393</td>\n",
       "      <td>131.142820</td>\n",
       "      <td>204.162670</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>0.0</td>\n",
       "      <td>53.512720</td>\n",
       "      <td>2017-01-01 00:00:00</td>\n",
       "      <td>98.870458</td>\n",
       "      <td>62.356422</td>\n",
       "      <td>69.056423</td>\n",
       "      <td>55.516887</td>\n",
       "      <td>61.839855</td>\n",
       "      <td>99.557440</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>0.0</td>\n",
       "      <td>6.143042</td>\n",
       "      <td>2017-01-01 00:00:00</td>\n",
       "      <td>23.472831</td>\n",
       "      <td>11.211765</td>\n",
       "      <td>10.880237</td>\n",
       "      <td>10.712582</td>\n",
       "      <td>6.077408</td>\n",
       "      <td>13.165344</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>0.0</td>\n",
       "      <td>101.701470</td>\n",
       "      <td>2017-01-01 00:00:00</td>\n",
       "      <td>276.100799</td>\n",
       "      <td>154.241294</td>\n",
       "      <td>251.449750</td>\n",
       "      <td>353.429545</td>\n",
       "      <td>335.934840</td>\n",
       "      <td>302.159640</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>0.0</td>\n",
       "      <td>1141.240666</td>\n",
       "      <td>2017-01-01 00:00:00</td>\n",
       "      <td>1159.726712</td>\n",
       "      <td>860.610848</td>\n",
       "      <td>1112.319660</td>\n",
       "      <td>1332.812678</td>\n",
       "      <td>1362.286000</td>\n",
       "      <td>1109.127600</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11937496</th>\n",
       "      <td>1363</td>\n",
       "      <td>0.0</td>\n",
       "      <td>184.524994</td>\n",
       "      <td>2018-12-31 19:00:00</td>\n",
       "      <td>271.613198</td>\n",
       "      <td>247.946070</td>\n",
       "      <td>278.245812</td>\n",
       "      <td>195.938456</td>\n",
       "      <td>275.569240</td>\n",
       "      <td>198.379180</td>\n",
       "      <td>41497410</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11937497</th>\n",
       "      <td>1363</td>\n",
       "      <td>0.0</td>\n",
       "      <td>183.600006</td>\n",
       "      <td>2018-12-31 20:00:00</td>\n",
       "      <td>269.931399</td>\n",
       "      <td>244.945510</td>\n",
       "      <td>275.208346</td>\n",
       "      <td>187.017012</td>\n",
       "      <td>268.388500</td>\n",
       "      <td>194.510350</td>\n",
       "      <td>41497660</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11937498</th>\n",
       "      <td>1363</td>\n",
       "      <td>0.0</td>\n",
       "      <td>178.475006</td>\n",
       "      <td>2018-12-31 21:00:00</td>\n",
       "      <td>261.406075</td>\n",
       "      <td>227.145438</td>\n",
       "      <td>260.905062</td>\n",
       "      <td>179.807843</td>\n",
       "      <td>269.436900</td>\n",
       "      <td>184.452240</td>\n",
       "      <td>41497910</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11937499</th>\n",
       "      <td>1363</td>\n",
       "      <td>0.0</td>\n",
       "      <td>179.725006</td>\n",
       "      <td>2018-12-31 22:00:00</td>\n",
       "      <td>238.664772</td>\n",
       "      <td>217.781872</td>\n",
       "      <td>245.831210</td>\n",
       "      <td>178.231907</td>\n",
       "      <td>254.699950</td>\n",
       "      <td>180.633790</td>\n",
       "      <td>41498160</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11937500</th>\n",
       "      <td>1363</td>\n",
       "      <td>0.0</td>\n",
       "      <td>175.324997</td>\n",
       "      <td>2018-12-31 23:00:00</td>\n",
       "      <td>217.100026</td>\n",
       "      <td>212.569038</td>\n",
       "      <td>235.722034</td>\n",
       "      <td>167.573243</td>\n",
       "      <td>226.425600</td>\n",
       "      <td>167.587310</td>\n",
       "      <td>41498410</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>11937501 rows × 12 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "          building_id  meter  meter_reading           timestamp        pred1  \\\n",
       "0                   0    0.0     173.370293 2017-01-01 00:00:00   196.794798   \n",
       "1                   1    0.0      53.512720 2017-01-01 00:00:00    98.870458   \n",
       "2                   2    0.0       6.143042 2017-01-01 00:00:00    23.472831   \n",
       "3                   3    0.0     101.701470 2017-01-01 00:00:00   276.100799   \n",
       "4                   4    0.0    1141.240666 2017-01-01 00:00:00  1159.726712   \n",
       "...               ...    ...            ...                 ...          ...   \n",
       "11937496         1363    0.0     184.524994 2018-12-31 19:00:00   271.613198   \n",
       "11937497         1363    0.0     183.600006 2018-12-31 20:00:00   269.931399   \n",
       "11937498         1363    0.0     178.475006 2018-12-31 21:00:00   261.406075   \n",
       "11937499         1363    0.0     179.725006 2018-12-31 22:00:00   238.664772   \n",
       "11937500         1363    0.0     175.324997 2018-12-31 23:00:00   217.100026   \n",
       "\n",
       "               pred2        pred3        pred4        pred5        pred6  \\\n",
       "0          82.730302   134.285627   121.220393   131.142820   204.162670   \n",
       "1          62.356422    69.056423    55.516887    61.839855    99.557440   \n",
       "2          11.211765    10.880237    10.712582     6.077408    13.165344   \n",
       "3         154.241294   251.449750   353.429545   335.934840   302.159640   \n",
       "4         860.610848  1112.319660  1332.812678  1362.286000  1109.127600   \n",
       "...              ...          ...          ...          ...          ...   \n",
       "11937496  247.946070   278.245812   195.938456   275.569240   198.379180   \n",
       "11937497  244.945510   275.208346   187.017012   268.388500   194.510350   \n",
       "11937498  227.145438   260.905062   179.807843   269.436900   184.452240   \n",
       "11937499  217.781872   245.831210   178.231907   254.699950   180.633790   \n",
       "11937500  212.569038   235.722034   167.573243   226.425600   167.587310   \n",
       "\n",
       "            row_id  site_id  \n",
       "0                0        0  \n",
       "1                1        0  \n",
       "2                2        0  \n",
       "3                3        0  \n",
       "4                4        0  \n",
       "...            ...      ...  \n",
       "11937496  41497410       15  \n",
       "11937497  41497660       15  \n",
       "11937498  41497910       15  \n",
       "11937499  41498160       15  \n",
       "11937500  41498410       15  \n",
       "\n",
       "[11937501 rows x 12 columns]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "leak_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sko.PSO import PSO\n",
    "from sko.GA import GA\n",
    "# Define the genetic algorithm function\n",
    "def optimization(p):\n",
    "    w1,w2,w3,w4,w5,w6 = p\n",
    "    v = w1 * leak_df['pred1'].values + w2 * leak_df['pred2'].values + w3 * leak_df['pred3'] +\\\n",
    "    w4 * leak_df['pred4'].values + w5 * leak_df['pred5'].values + w6 * leak_df['pred6']\n",
    "    vl1p = np.log1p(v)\n",
    "    print(np.sqrt(mean_squared_error(vl1p, leak_df.meter_reading_l1p)))\n",
    "    return np.sqrt(mean_squared_error(vl1p, leak_df.meter_reading_l1p))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.109737976015069\n",
      "1.071967825760083\n",
      "1.009634291515726\n",
      "1.0602919102496149\n",
      "0.9950684836406741\n",
      "1.1528982240925243\n",
      "1.020739429068769\n",
      "1.2836804753728865\n",
      "0.9704735033863185\n",
      "1.1416376890257836\n",
      "1.0065571252861902\n",
      "1.189731310079097\n",
      "1.0797603426075733\n",
      "1.1301383242364194\n",
      "1.1686875150064888\n",
      "0.9682509576139967\n",
      "1.1957461385869983\n",
      "1.1412918637774978\n",
      "1.0886561175828984\n",
      "1.1614582069958226\n",
      "0.9827337167035173\n",
      "1.2518159375433986\n",
      "1.2650653212118772\n",
      "1.2288951759484743\n",
      "1.2121881049880472\n",
      "1.044555622440699\n",
      "0.9909368117998765\n",
      "1.003520317984029\n",
      "1.1196916185244783\n",
      "1.1082087077841833\n",
      "1.0663890810809376\n",
      "1.1716893643061608\n",
      "1.1573231566378412\n",
      "1.1430672722410262\n",
      "1.1252819988262788\n",
      "0.9947758698792276\n",
      "1.2154651555819007\n",
      "1.078155950875734\n",
      "1.0357618186686322\n",
      "1.0275512184023252\n",
      "1.0999413138079304\n",
      "1.1342679687175607\n",
      "0.9727911805687389\n",
      "1.0646035246892527\n",
      "1.181390346608529\n",
      "1.0161485493232443\n",
      "0.9943709833489309\n",
      "1.3060904980027233\n",
      "1.2094952243933599\n",
      "1.121675883540353\n",
      "1.1587971983839018\n",
      "1.0007995606360416\n",
      "1.119933874116876\n",
      "1.0202305681301251\n",
      "1.1157013755212193\n",
      "1.0979838836010882\n",
      "1.1435591296772878\n",
      "1.0775288540669885\n",
      "1.13518779119216\n",
      "0.9802333064531297\n",
      "1.1006773450629823\n",
      "1.041982929107624\n",
      "1.0102278367658781\n",
      "0.9995718933302887\n",
      "1.0997783308225928\n",
      "1.0682450486567654\n",
      "1.3087313988246754\n",
      "1.1368330842436296\n",
      "1.1973014327745948\n",
      "1.1628075859785825\n",
      "1.026979752818011\n",
      "1.1843706975059294\n",
      "1.0336390173960348\n",
      "1.231867860657018\n",
      "1.059402006947919\n",
      "0.9699050462787178\n",
      "1.1618081447600535\n",
      "1.1305477070297538\n",
      "0.9731443692724836\n",
      "0.9899166642073501\n",
      "1.1031533006375585\n",
      "1.0592930943310161\n",
      "1.209525573381485\n",
      "1.115538669984469\n",
      "1.3654780958039663\n",
      "1.1239201700354522\n",
      "1.0123436681591316\n",
      "1.0522778441013267\n",
      "1.1045798660609716\n",
      "1.0976531751767806\n",
      "1.1379811837760758\n",
      "1.0930450446784252\n",
      "1.1927915036420742\n",
      "0.9872909343466213\n",
      "1.2923287982209564\n",
      "0.9757086460464129\n",
      "1.146898902777596\n",
      "0.965581592216071\n",
      "1.107124205839683\n",
      "1.1198037682426294\n",
      "1.0068766960313864\n",
      "1.1483022694727598\n",
      "0.998694488134759\n",
      "0.9746213199526446\n",
      "1.0084211324928058\n",
      "1.0473102093077835\n",
      "0.9935864903993633\n",
      "0.9828758307863595\n",
      "0.9725327685180221\n",
      "0.971626388664583\n",
      "1.0296596977013568\n",
      "1.0331254268949657\n",
      "0.9827335883668421\n",
      "1.0189463531430751\n",
      "1.0681425758527987\n",
      "1.1999851841673663\n",
      "1.1602319771406322\n",
      "0.9797041840849389\n",
      "0.9872919533750762\n",
      "1.025878647127701\n",
      "1.180130908274803\n",
      "0.9947758698792276\n",
      "1.0839096600457834\n",
      "0.9828241371685849\n",
      "1.0807656817169544\n",
      "1.1139828993930245\n",
      "1.0170527682124872\n",
      "1.0317517713359563\n",
      "1.1235382261681577\n",
      "1.1071439596600645\n",
      "0.9761933483824807\n",
      "1.0521671847950618\n",
      "0.99779013815763\n",
      "1.0788307940914903\n",
      "0.9953141849974165\n",
      "1.0793283649587462\n",
      "1.1332319085551403\n",
      "1.0202169741753426\n",
      "1.0044178912520936\n",
      "1.0269232005606235\n",
      "0.9745567717081015\n",
      "1.0342511397783454\n",
      "1.00043559969694\n",
      "1.2482173618394614\n",
      "1.0397133765099347\n",
      "0.9887931608448003\n",
      "1.0134180124276666\n",
      "1.0645347445811364\n",
      "0.968187444061334\n",
      "0.9794952841073484\n",
      "0.9773168537719565\n",
      "0.9733698002376668\n",
      "1.1160081814037535\n",
      "1.062667700488494\n",
      "1.0356697355637086\n",
      "0.9717092703790569\n",
      "1.1163362387478613\n",
      "1.0164075330950864\n",
      "1.0775649702758585\n",
      "0.9706824757529638\n",
      "1.0674435743810247\n",
      "1.0680290213435302\n",
      "1.071968094074167\n",
      "1.0113072020070273\n",
      "0.96896424956969\n",
      "0.9987658735369718\n",
      "0.9815241003467668\n",
      "0.9811813027123987\n",
      "0.9872909343466213\n",
      "0.9890287401245261\n",
      "0.9759505702218537\n",
      "0.9682509576139967\n",
      "0.9881950697978168\n",
      "0.9769913869757966\n",
      "0.9932809697578504\n",
      "1.002002574042753\n",
      "0.969095917369331\n",
      "0.983782753079348\n",
      "0.9796418149893814\n",
      "0.9803524572291678\n",
      "1.010548414540677\n",
      "0.9916501385206287\n",
      "1.065610726960665\n",
      "0.9694164630585848\n",
      "1.146201614468711\n",
      "1.0095194387070894\n",
      "1.0089036801735707\n",
      "1.0602763460142912\n",
      "1.1151325361521232\n",
      "0.9688642270179801\n",
      "1.083312610277206\n",
      "1.097883723397366\n",
      "1.0273274641956776\n",
      "0.9718754529592024\n",
      "1.003031420364374\n",
      "1.0528928834951687\n",
      "0.9689990376363645\n",
      "0.9756878504224532\n",
      "1.0767785080949783\n",
      "1.14769339315078\n",
      "1.009716278115188\n",
      "0.9808935015982414\n",
      "1.0080977735262444\n",
      "1.1599319718053311\n",
      "1.081240865376276\n",
      "1.0140151719024684\n",
      "0.9655148970102874\n",
      "0.9688447516868692\n",
      "0.9884322552994285\n",
      "0.9800798431350488\n",
      "0.9877734481396858\n",
      "0.9883822931573756\n",
      "1.004717938029973\n",
      "1.0088927902679825\n",
      "0.9939842161076549\n",
      "1.1581445174330507\n",
      "0.9731971987879817\n",
      "1.011166803141616\n",
      "1.0220130595399064\n",
      "0.9975938350949988\n",
      "1.0108022615611083\n",
      "0.9786534332927713\n",
      "1.0087546176829945\n",
      "0.9736985321946569\n",
      "0.9729080725252032\n",
      "1.0151282995938435\n",
      "0.9687355125635579\n",
      "0.9722068512099722\n",
      "0.9684936438448106\n",
      "0.9980286406737775\n",
      "1.000438928544098\n",
      "0.9759930932920677\n",
      "0.9755610565160904\n",
      "0.9697548740969592\n",
      "0.9674550676039703\n",
      "0.9757775834601916\n",
      "0.967828982624912\n",
      "1.010424538347711\n",
      "1.056838638531428\n",
      "0.9783251573599326\n",
      "0.9817188952218524\n",
      "1.0525620295036704\n",
      "1.055339708251656\n",
      "1.0182204219844189\n",
      "1.0052443124158605\n",
      "1.0085126419044526\n",
      "0.9682074919847699\n",
      "0.9882074868587536\n",
      "1.0624671472222988\n",
      "1.0739758662385197\n",
      "0.9653661382778679\n",
      "0.9898701523523802\n",
      "0.969301564137395\n",
      "1.1423141546026878\n",
      "0.9937609053722851\n",
      "0.9963907110163309\n",
      "1.0359669054422482\n",
      "0.9762626321800072\n",
      "1.0984474051655007\n",
      "1.0162330418930223\n",
      "1.0493961084959906\n",
      "1.0197448160286713\n",
      "0.9669600420853842\n",
      "0.9888011031085002\n",
      "0.9719657237361045\n",
      "1.0005634908775767\n",
      "0.9919117333640155\n",
      "1.0063022064657985\n",
      "1.057632167354778\n",
      "0.9801497710895368\n",
      "1.0664806738473962\n",
      "0.974543261152217\n",
      "1.048351839290237\n",
      "0.9769760475781113\n",
      "0.9805242330293183\n",
      "0.9776380030978705\n",
      "1.0207997265998408\n",
      "0.9682283999211654\n",
      "1.0244170158441512\n",
      "0.9772928955981207\n",
      "0.971625192511597\n",
      "0.986028764590732\n",
      "1.0013222025246973\n",
      "0.9781309327098937\n",
      "0.9977635422810983\n",
      "1.0167912655877682\n",
      "0.9879872651978944\n",
      "0.9739265363869763\n",
      "0.9692153908760148\n",
      "0.9812224694670971\n",
      "0.9792965991822706\n",
      "1.0860091288347202\n"
     ]
    }
   ],
   "source": [
    "#Run this: PSO algorithm iter 100 test to test the score\n",
    "ga = GA(func=optimization, n_dim=6, size_pop=100, max_iter=100, lb=[0,0,0,0,0,0], ub=[0.5,0.5,0.5,0.5,0.5,0.5], precision=1e-7)\n",
    "ga.run()\n",
    "print('best_x is ', pso.gbest_x, 'best_y is', pso.gbest_y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "best_x is  [0.5        0.         0.         0.09939658 0.20748924 0.08029802] best_y is 0.9634\n"
     ]
    }
   ],
   "source": [
    "print('best_x is ', pso.gbest_x, 'best_y is', pso.gbest_y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "w1 = 0.5 \n",
    "w2 = 0.01895657\n",
    "w3 = 0.00168529\n",
    "w4 = 0.25909864\n",
    "w5 = 0.12004669\n",
    "w6 = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.963"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "v = w1 * leak_df['pred1'].values + w2 * leak_df['pred2'].values + w3 * leak_df['pred3'] +\\\n",
    "w4 * leak_df['pred4'].values + w5 * leak_df['pred5'].values + w6 * leak_df['pred6']\n",
    "vl1p = np.log1p(v)\n",
    "np.sqrt(mean_squared_error(vl1p, leak_df.meter_reading_l1p))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "sample_submission = pd.read_csv(path + 'sample_submission.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "sample_submission['meter_reading'] = w1 * test_df.pred1 +  w2 * test_df.pred3  + w3 * test_df.pred2+\\\n",
    "      w4 * test_df.pred4+w5 * test_df.pred5+w6 * test_df.pred6\n",
    "sample_submission.loc[sample_submission.meter_reading < 0, 'meter_reading'] = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Reindex the row_id, put the meter_reading into sample_submission\n",
    "leak_df = leak_df[['meter_reading', 'row_id']].set_index('row_id').dropna()\n",
    "sample_submission.loc[leak_df.index, 'meter_reading'] = leak_df['meter_reading']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Save the submission\n",
    "sample_submission.to_csv('../../Large_output/ensemble_pso_second_try.csv', index=False, float_format='%.4f')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sample_submission.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
